package com.ewanghuiju.app.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.github.ielse.imagewatcher.ImageWatcher;
import com.github.ielse.imagewatcher.ImageWatcherHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class GlideSimpleLoader implements ImageWatcher.Loader {
    @Override
    public void load(Context context, Uri uri, final ImageWatcher.LoadCallback lc) {
//        Glide.with(context).load(uri)
//                .into(new SimpleTarget<GlideDrawable>() {
//                    @Override
//                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
//                        lc.onResourceReady(resource);
//                        LogUtil.e("onResourceReady");
//                    }
//
//                    @Override
//                    public void onLoadStarted(Drawable placeholder) {
//                        lc.onLoadStarted(placeholder);
//                        LogUtil.e("onLoadStarted");
//                    }
//
//                    @Override
//                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
//                        lc.onLoadFailed(errorDrawable);
//                    }
//                });
        Glide.with(context).load(uri)
                .into(new CustomTarget<Drawable>() {

                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        lc.onResourceReady(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                        lc.onLoadStarted(placeholder);
                    }
                });
    }

    ImageWatcherHelper iwHelper;

    public void onCreate(Activity activity) {
        iwHelper = ImageWatcherHelper.with(activity, new GlideSimpleLoader());
    }

    public boolean onBackPressed() {
        return iwHelper.handleBackPressed();

    }

    public void show(List<String> list, int pos) {

        List<Uri> dataList = new ArrayList<>();
        for (String str : list) {
            if (!TextUtils.isEmpty(str)) {
                Uri uri = Uri.fromFile(new File(str));
                dataList.add(uri);
            }

        }
        iwHelper.show(dataList, pos);

    }

    public void showUrl(List<String> list, int pos) {

        List<Uri> dataList = new ArrayList<>();
        for (String str : list) {
            if (!TextUtils.isEmpty(str)) {
                Uri uri = Uri.parse(str);
                dataList.add(uri);
            }

        }
        iwHelper.show(dataList, pos);

    }
}